<template>
  <div id="secondTopSearch">
    <div class="search-box">
      <el-form
        ref="ruleForm"
        :inline="true"
        :model="formInline"
        :rules="rules"
        class="demo-form-inline"
      >
        <el-form-item label="入量上级：" prop="checkStatu">
          <el-select v-model="formInline.utmCampaign" placeholder="">
            <el-option
              v-for="item in chooseShareList"
              :key="item.id"
              :label="item.portName"
              :value="item.id"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-select v-model="formInline.utmSource" placeholder="">
            <el-option
              v-for="item in shareList"
              :key="item.id"
              :label="item.portName"
              :value="item.id"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="日期" prop="createTime">
          <el-date-picker
            value-format="yyyy-MM-dd"
            class="data-input"
            v-model="formInline.statisticDate"
            type="date"
            placeholder="选择日期"
            :clearable="false"
            :picker-options="pickerTopDataDay"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item class="s-btn">
          <el-button type="success" icon="el-icon-search" @click="toSearch()">
            查询
          </el-button>
          <el-button type="success" plain @click="reset">
            重置
          </el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
import { getDay } from '@utils/yesterday'
import { getPort } from '@/api/shareBackFlow/eachPortShare'
import { deepClone } from '@utils/deepClone'
export default {
  props: {
    formInline: {
      type: Object,
      default: function() {
        return {
          // topPort: '',
          sourcePort: '',
          statisticDate: '',
          utmSource: '',
          utmCampaign: ''
        }
      }
    }
  },
  data() {
    return {
      chooseShareList: [],
      shareList: [],
      rules: {
        activityId: {},
        type: {}
      },
      pickerTopDataDay: {
        disabledDate(time) {
          //昨天之后不可选
          const yesterday = new Date() - 3600 * 1000 * 24 * 1
          return time.getTime() > yesterday
        }
      }
    }
  },
  watch: {
    'formInline.utmCampaign': {
      handler(newV, oldV) {
        if (newV) {
          this.getShareList(true)
        }
        if (newV === '') {
          //全部
          this.shareList = [{ id: '', portName: '全部' }]
          this.formInline.utmSource = ''
        }
      },
      deep: true
    }
  },
  mounted() {
    this.init()
  },
  methods: {
    async init() {
      this.chooseShareList = await this.getTopList()
      this.$nextTick(() => {
        this.getShareList()
      })
    },
    getShareList(setDefault = false) {
      if (this.chooseShareList.length === 0) return
      let _this = this
      this.chooseShareList.forEach((item, index) => {
        if (item.id == _this.formInline.utmCampaign) {
          //this.shareList = item.childList
          _this.shareList = [].concat(
            { id: '', portName: '全部' },
            item.childList
          )
        }
        this.formInline.utmSource = this.shareList[0].id
      })
      if (setDefault) {
        this.formInline.utmSource = this.shareList[0].id
      }
    },
    getTopList() {
      let _this = this
      return new Promise((resolve, reject) => {
        let params = { type: 1 }
        getPort(
          params,
          (res) => {
            let a = deepClone(res)
            let obj = { id: '', portName: '全部', childList: [] }
            a.unshift(obj)
            _this.formInline.utmCampaign = a[0].id
            resolve(a)
          },
          (res) => {
            this.$message({
              message: res.message,
              type: 'warning'
            })
            resolve([])
          }
        )
      })
    },
    /* 搜索 */
    toSearch() {
      this.$emit('toSearch')
    },
    /* 重置 */
    reset() {
      this.$refs['ruleForm'].resetFields()
      this.setDefaultValue()
    },
    setDefaultValue() {
      this.formInline.statisticDate = getDay(-1, '-')
      this.formInline.utmCampaign = ''
      this.formInline.utmSource = ''
      // this.$emit('toSearch')
    }
  }
}
</script>

<style lang="less"></style>
